<template>
  <a-modal
    title="编辑user"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['userId']"/>
        </a-form-item>

        <a-form-item
          label="用户编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入用户编号" v-decorator="['userId', {rules: [{required: true, message: '请输入用户编号！'}]}]" />
        </a-form-item>

        <a-form-item
          label="用户名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入用户名" v-decorator="['userName', {rules: [{required: true, message: '请输入用户名！'}]}]" />
        </a-form-item>

        <a-form-item
          label="密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入密码" v-decorator="['password', {rules: [{required: true, message: '请输入密码！'}]}]" />
        </a-form-item>

          <a-form-item
            label="性别"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback=""
          >
            <a-select style="width: 100%" placeholder="请选择性别" v-decorator="['sex', {rules: [{ required: true, message: '请选择性别！' }]}]" >
              <a-select-option v-for='(item,index) in sex' :key="index" :value="item.code" >{{item.name}}</a-select-option>
            </a-select>
          </a-form-item>

        <a-form-item
          label="头像"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入头像" v-decorator="['userImage', {rules: [{required: true, message: '请输入头像！'}]}]" />
        </a-form-item>

          <a-form-item
            label="生日"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <a-date-picker show-time>
              <template slot="renderExtraFooter">
                选择日期
              </template>
            </a-date-picker>
          </a-form-item>

        <a-form-item
          label="昵称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入昵称" v-decorator="['nick', {rules: [{required: true, message: '请输入昵称！'}]}]" />
        </a-form-item>

          <a-form-item
            label="是否为管理员"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback=""
          >
            <a-select style="width: 100%" placeholder="请选择是否为管理员" v-decorator="['power', {rules: [{ required: true, message: '请选择是否为管理员！' }]}]" >
              <a-select-option v-for='(item,index) in power' :key="index" :value="item.code" >{{item.name}}</a-select-option>
            </a-select>
          </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/user/userManage'

  export default {
    data() {
      return {
        sex: [{"name":"女","value":0},{"name":"男","value":1}],
        power: [{"name":"普通用户","value":0},{"name":"管理员","value":1}],
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 15}
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      edit(record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue({
            userId: record.userId,
            userName: record.userName,
            password: record.password,
            sex: record.sex,
            userImage: record.userImage,
            birthday: record.birthday,
            nick: record.nick,
            power: record.power,
          });
        }, 100)
      },
      handleSubmit() {
        const {form: {validateFields}} = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel() {
        this.form.resetFields();
        this.visible = false
      }
    }
  }
</script>
